<style lang='scss' scoped>
	.search {
		font-size: 20rpx;

		.nav {
			display: flex;
			align-items: center;
			height: 100rpx;

			.back {
				width: 80rpx;
				text-align: center;
			}

			.input {
				flex: 1;
				display: flex;
				background-color: #ccc;
				padding: 10rpx;
				border-radius: 20rpx;

				text {
					font-size: 30rpx;
				}

				input {
					flex: 1;
				}
			}

			.search {
				width: 80rpx;
				text-align: center;
			}
		}

		.lishi {
			padding: 20rpx;

			.title {
				font-size: 40rpx;
				font-weight: 500;
				display: flex;
				justify-content: space-between;
			}

			.awrp {
				display: flex;
				flex-wrap: wrap;

				text {
					background-color: #ccc;
					border-radius: 30rpx;
					margin-right: 20rpx;
					padding: 0 10rpx;
					margin-bottom: 10rpx;
				}
			}

		}
	}
</style>
<template>
	<view class='search'>
		<view class="nav">
			<view class="back" @click="$router.back()">
				返回
			</view>
			<view class="input">
				<text>Q</text>
				<input placeholder="请输入关键字" focus v-model="inputVal" @confirm="search">
			</view>
			<view class="search">
				搜索
			</view>
		</view>
		<div class="lishi">
			<view class="title">
				<text>历史记录</text>
				<text @click="qkong">清空历史记录</text>
			</view>
			<view class="awrp">
				<text v-for="(item,index) in lishiList" :key="index">{{item}}</text>
			</view>
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				inputVal: '',
				lishiList: []
			}
		},
		onLoad(o) {},
		onShow() {
			this.lishiList = uni.getStorageSync('lishiList') || []
		},
		methods: {
			search() {
				if (this.inputVal == '') {
					uni.showToast({
						title: '请输入内容啊你他妈的',
						icon: "none"
					})
					return
				}
				let index = this.lishiList.findIndex(v => v == this.inputVal)
				if (index == -1) {
					this.lishiList.unshift(this.inputVal)
				} else {
					this.lishiList.splice(index, 1)
					this.lishiList.unshift(this.inputVal)
				}
				uni.setStorageSync('lishiList', this.lishiList)

				uni.navigateTo({
					url: `/pages/search-result/search-result?val=${this.inputVal}`
				})

				this.inputVal = ''
			},
			qkong() {
				uni.setStorageSync('lishiList', [])
				this.lishiList = []
			}
		}
	}
</script>
